package com.dnjn.happiness.biz.controller;

import com.dnjn.happiness.biz.domain.TrainingRecord;
import com.dnjn.happiness.biz.mapper.TrainingRecordMapper;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.HxBeanSeed;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.api.RemoteEmoUserService;
import com.dnjn.happiness.system.api.domain.SysUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "biz training qrcode 培训二维码", description = "培训二维码")
@Slf4j
@RestController
@RequestMapping("/TrainingQRCode")
public class TrainingQRCodeController extends BaseController {
    @Autowired
    ObtainWxQRCode obtainWxQRCode;

    @Autowired
    private TrainingRecordMapper trainingRecordMapper;

    @Autowired
    private RemoteEmoUserService hxBeanService;


    @Operation(summary = "生成二维码")
    @GetMapping("/wxTrainingCode/{trainingId}")
    public String wxTrainingCode(@PathVariable Long trainingId) {

        String path = "pages/signin?trainingId=" + trainingId;
        String wxCode = obtainWxQRCode.getWxCode(path);
        return wxCode;


    }

    //增加培训记录(签到)
    @Operation(summary = "扫码签到")
    @GetMapping("/pages/signinTable")
    public void myRecord(Long trainingId) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        TrainingRecord trainingRecord = new TrainingRecord();
        trainingRecord.setTrainingId(trainingId);
        trainingRecord.setUserId(user.getUserId());
        trainingRecord.setTrainingRecordStatus("Y");
        trainingRecordMapper.insert(trainingRecord);
        // 完成一次培训新增快乐豆
        HxBeanSeed beanSeed = new HxBeanSeed();
        beanSeed.setUserId(user.getUserId());
        beanSeed.setRiskType(Constants.TRAINING_HLBEAN_CODE);
        R<Boolean> result = hxBeanService.updateHBean(beanSeed, SecurityConstants.INNER);

    }


}
